package global

import (
	"sync"

	"uploadSvc/utils/timer"

	"go.uber.org/zap"

	"uploadSvc/config"

	"github.com/spf13/viper"
	"gorm.io/gorm"
)

var (
	NS_DB     *gorm.DB
	NS_DBList map[string]*gorm.DB
	NS_CONFIG config.Server
	NS_VP     *viper.Viper
	// NS_LOG    *oplogging.Logger
	NS_LOG   *zap.Logger
	NS_Timer timer.Timer = timer.NewTimerTask()

	lock sync.RWMutex
)

// GetGlobalDBByDBName 通过名称获取db list中的db
func GetGlobalDBByDBName(dbname string) *gorm.DB {
	lock.RLock()
	defer lock.RUnlock()
	return NS_DBList[dbname]
}

// MustGetGlobalDBByDBName 通过名称获取db 如果不存在则panic
func MustGetGlobalDBByDBName(dbname string) *gorm.DB {
	lock.RLock()
	defer lock.RUnlock()
	db, ok := NS_DBList[dbname]
	if !ok || db == nil {
		panic("db no init")
	}
	return db
}
